package com.xbts.web.controller.cloud;

import com.alibaba.fastjson2.JSONObject;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.bss.v2.region.BssRegion;
import com.huaweicloud.sdk.bss.v2.*;
import com.huaweicloud.sdk.bss.v2.model.*;
import com.xbts.common.config.XbtsConfig;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.SettingEnum;
import com.xbts.common.utils.bean.BeanUtils;
import com.xbts.system.domain.dto.AccountSetting;
import com.xbts.system.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 查询资源类型列表
 */
@RestController
@RequestMapping("/cloud")
public class ListResourceTypesSolution extends BaseController {

    @Autowired
    private SettingService settingService;

    @RequestMapping("/listResourceTypes")
    public AjaxResult listResourceTypes(@RequestBody Object jsonObject) {
        AccountSetting accountSetting = settingService.getAccountSetting(SettingEnum.HUAWEI);

        ICredential auth = new GlobalCredentials()
                .withAk(accountSetting.getAccessKeyId())
                .withSk(accountSetting.getSecretAccessKey());

        BssClient client = BssClient.newBuilder()
                .withCredential(auth)
                .withRegion(BssRegion.valueOf("cn-north-1"))
                .build();

        ListResourceTypesRequest request = BeanUtils.formatKeyValueCloud(jsonObject, ListResourceTypesRequest.class);
        System.err.println("request = "+ request.toString());
        ListResourceTypesResponse response = null;
        try {
            response = client.listResourceTypes(request);
        } catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) {
            e.printStackTrace();
        }
        return success(response);
    }
}